﻿#include "CDevSensorSeries.h"
#include "CSqlOper.h"
#include "Public.h"
#include "CGlobal.h"
#include "CSensorSericesCfgOper.h"
#include "CAlarmPoint.h"

CDevSensorSeries::CDevSensorSeries(QString sDevGuid, QString sDevName, bool& bRet)
{
	m_sDevGuid = sDevGuid;
	m_sDevName = sDevName;
	bRet=init(sDevGuid);
}

CDevSensorSeries::~CDevSensorSeries()
{
	if (m_pCtrMange != nullptr)
	{
		delete m_pCtrMange;
		m_pCtrMange = nullptr;
	}
	if (m_pDevData != nullptr)
	{
		delete m_pDevData;
		m_pDevData = nullptr;
	}
}

bool CDevSensorSeries::ctrDev(SCtrInfo stCtrInfo)
{
	//设备本体
	if (stCtrInfo.m_nCtrDevType == enOntology)
	{
		return m_pCtrMange->CtrDevice(stCtrInfo);
	}
	return false;
}

bool CDevSensorSeries::init(QString sDevGuid)
{
	m_pDevData = new CDevDataSensorSeries();
	m_pDevData->m_sDevGuid = sDevGuid;
	//首先查询是否存在这个声振温设备的配置
	{
		QString sErrMsg = "";
		QVector<QSqlRecord> vecRecord;
		bool bSql = CSqlOper::instance().selectSensorSeriesInfo(sDevGuid, vecRecord, sErrMsg);   //创建设备数据
		if (!bSql)
		{
			return false;
		}
		if (vecRecord.size() == 0)//没查到
		{
			CSensorSericesCfgOper oper(sDevGuid);
			int nRet = oper.exec();
			if (!nRet == QDialog::Accepted)
			{
				return false;
			}
		}
	}
	QString sErrMsg = "";
	bool bRet = CSqlOper::instance().selectSensorSeriesInfo(sDevGuid, m_pDevData, sErrMsg);   //创建设备数据
	if (!bRet)
	{
		logText(A2T("创建设备数据失败"));
		return false;
	}
	//通道配置表
	{
		QString sErrMsg = "";
		QVector<QSqlRecord> vecRecord;
		CSqlOper::instance().selectSensorSeriesChannelInfo(sDevGuid, vecRecord, sErrMsg);   //创建设备数据
		if (vecRecord.size() == 0)//没查到
		{
			//添加默认的设备配置到pai_sensor_series_config表中
			bool bRet=CSqlOper::instance().insertDefChannelCfg(sDevGuid, sErrMsg);   //创建设备数据
			if (!bRet)
			{
				logText(A2T("创建默认通道失败"));
			}
		}
	}
	bRet=CSqlOper::instance().selectSensorSeriesChannelInfo(sDevGuid, m_pDevData, sErrMsg);   //创建设备数据
	if (!bRet)
	{
		return false;
	}
	//初始化设备测点集合
	CAlarmPoint* pPointConnectState = new CAlarmPoint("ConnectState", A2T("连接状态"), NULL,EPointType::enDigital, "");
	pPointConnectState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enConnectState, pPointConnectState);
	if (m_pDevData->m_mapChannel[1].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel1State = new CAlarmPoint("Channel1State", A2T("通道1状态"), NULL, EPointType::enDigital, "");
		pPointChannel1State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel1State, pPointChannel1State);

		CAlarmPoint* pPointChannel1Temp = new CAlarmPoint("Channel1Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[1].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel1Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel1Temp, pPointChannel1Temp);

		CAlarmPoint* pPointChannel1Vibration = new CAlarmPoint("Channel1Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[1].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel1Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel1Vibration, pPointChannel1Vibration);

		CAlarmPoint* pPointChannel1Voice = new CAlarmPoint("Channel1Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[1].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel1Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel1Voice, pPointChannel1Voice);
	}
	if (m_pDevData->m_mapChannel[2].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel2State = new CAlarmPoint("Channel2State", A2T("通道2状态"), NULL, EPointType::enDigital, "");
		pPointChannel2State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel2State, pPointChannel2State);

		CAlarmPoint* pPointChannel2Temp = new CAlarmPoint("Channel2Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[2].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel2Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel2Temp, pPointChannel2Temp);

		CAlarmPoint* pPointChannel2Vibration = new CAlarmPoint("Channel2Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[2].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel2Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel2Vibration, pPointChannel2Vibration);

		CAlarmPoint* pPointChannel2Voice = new CAlarmPoint("Channel2Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[2].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel2Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel2Voice, pPointChannel2Voice);
	}
	if (m_pDevData->m_mapChannel[3].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel3State = new CAlarmPoint("Channel3State", A2T("通道3状态"), NULL, EPointType::enDigital, "");
		pPointChannel3State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel3State, pPointChannel3State);

		CAlarmPoint* pPointChannel3Temp = new CAlarmPoint("Channel3Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[3].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel3Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel3Temp, pPointChannel3Temp);

		CAlarmPoint* pPointChannel3Vibration = new CAlarmPoint("Channel3Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[3].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel3Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel3Vibration, pPointChannel3Vibration);

		CAlarmPoint* pPointChannel3Voice = new CAlarmPoint("Channel3Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[3].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel3Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel3Voice, pPointChannel3Voice);
	}
	if (m_pDevData->m_mapChannel[4].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel4State = new CAlarmPoint("Channel4State", A2T("通道4状态"), NULL, EPointType::enDigital, "");
		pPointChannel4State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel4State, pPointChannel4State);

		CAlarmPoint* pPointChannel4Temp = new CAlarmPoint("Channel4Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[4].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel4Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel4Temp, pPointChannel4Temp);

		CAlarmPoint* pPointChannel4Vibration = new CAlarmPoint("Channel4Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[4].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel4Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel4Vibration, pPointChannel4Vibration);

		CAlarmPoint* pPointChannel4Voice = new CAlarmPoint("Channel4Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[4].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel4Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel4Voice, pPointChannel4Voice);
	}
	if (m_pDevData->m_mapChannel[5].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel5State = new CAlarmPoint("Channel5State", A2T("通道5状态"), NULL, EPointType::enDigital, "");
		pPointChannel5State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel5State, pPointChannel5State);

		CAlarmPoint* pPointChannel5Temp = new CAlarmPoint("Channel5Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[5].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel5Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel5Temp, pPointChannel5Temp);

		CAlarmPoint* pPointChannel5Vibration = new CAlarmPoint("Channel5Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[5].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel5Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel5Vibration, pPointChannel5Vibration);

		CAlarmPoint* pPointChannel5Voice = new CAlarmPoint("Channel5Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[5].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel5Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel5Voice, pPointChannel5Voice);
	}
	if (m_pDevData->m_mapChannel[6].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel6State = new CAlarmPoint("Channel6State", A2T("通道6状态"), NULL, EPointType::enDigital, "");
		pPointChannel6State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel6State, pPointChannel6State);

		CAlarmPoint* pPointChannel6Temp = new CAlarmPoint("Channel6Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[6].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel6Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel6Temp, pPointChannel6Temp);

		CAlarmPoint* pPointChannel6Vibration = new CAlarmPoint("Channel6Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[6].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel6Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel6Vibration, pPointChannel6Vibration);

		CAlarmPoint* pPointChannel6Voice = new CAlarmPoint("Channel6Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[6].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel6Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel6Voice, pPointChannel6Voice);
	}
	if (m_pDevData->m_mapChannel[7].nIsEnable == 1)
	{
		CAlarmPoint* pPointChanne17State = new CAlarmPoint("Channel7State", A2T("通道7状态"), NULL, EPointType::enDigital, "");
		pPointChanne17State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel7State, pPointChanne17State);

		CAlarmPoint* pPointChannel7Temp = new CAlarmPoint("Channel7Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[7].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel7Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel7Temp, pPointChannel7Temp);

		CAlarmPoint* pPointChannel7Vibration = new CAlarmPoint("Channel7Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[7].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel7Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel7Vibration, pPointChannel7Vibration);

		CAlarmPoint* pPointChannel7Voice = new CAlarmPoint("Channel7Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[7].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel7Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel7Voice, pPointChannel7Voice);
	}
	if (m_pDevData->m_mapChannel[8].nIsEnable == 1)
	{
		CAlarmPoint* pPointChannel8State = new CAlarmPoint("Channel8State", A2T("通道8状态"), NULL, EPointType::enDigital, "");
		pPointChannel8State->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel8State, pPointChannel8State);

		CAlarmPoint* pPointChannel8Temp = new CAlarmPoint("Channel8Temp", A2T("%1温度状态").arg(m_pDevData->m_mapChannel[8].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel8Temp->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel8Temp, pPointChannel8Temp);

		CAlarmPoint* pPointChannel8Vibration = new CAlarmPoint("Channel8Vibration", A2T("%1振动状态").arg(m_pDevData->m_mapChannel[8].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel8Vibration->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel8Vibration, pPointChannel8Vibration);

		CAlarmPoint* pPointChannel8Voice = new CAlarmPoint("Channel8Voice", A2T("%1声音状态").arg(m_pDevData->m_mapChannel[8].sChannelName), NULL, EPointType::enDigital, "");
		pPointChannel8Voice->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enChannel8Voice, pPointChannel8Voice);
	}

	m_pCtrMange = new CSensorSeriesCtrMange(m_pDevData);//创建设备管理线程
	return true;
}
